// src/api/evaluation.ts
import http from './http';
import { requestWithRetry } from '@/utils/retry';

// 供应商评估接口定义
export interface SupplierEvaluation {
  id: number;
  supplierId: number;
  evaluator: string;
  evaluationDate: string;
  qualityScore: number;
  deliveryScore: number;
  serviceScore: number;
  priceScore: number;
  overallScore: number;
  evaluationPeriod: string;
  status: 'draft' | 'submitted' | 'approved';
  remark: string;
  createdAt: string;
  updatedAt: string;
  supplier?: {
    id: number;
    name: string;
  };
}

export interface EvaluationQueryParams {
  supplierId?: number;
  status?: string;
  page: number;
  limit: number;
}

export interface EvaluationListResponse {
  list: SupplierEvaluation[];
  pagination: {
    page: number;
    limit: number;
    total: number;
  };
}

export interface EvaluationStats {
  count: number;
  averageScore: number;
  latestEvaluation: {
    id: number;
    evaluationDate: string;
    overallScore: number;
    status: string;
  } | null;
}

// 获取评估列表
export const getEvaluations = async (params: EvaluationQueryParams) => {
  try {
    // 使用重试机制获取评估列表
    const response: any = await requestWithRetry(
      () => http.get('/procurement/evaluations', { params }),
      3, // 最多重试3次
      1000 // 基础延迟1秒
    );
    
    return {
      success: true,
      data: {
        list: response.data?.list || [],
        pagination: response.data?.pagination || {
          page: 1,
          limit: 20,
          total: 0
        }
      }
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取评估列表失败'
    };
  }
};

// 获取评估详情
export const getEvaluationDetail = async (id: number) => {
  try {
    // 使用重试机制获取评估详情
    const response = await requestWithRetry(
      () => http.get(`/procurement/evaluations/${id}`),
      3, // 最多重试3次
      1000 // 基础延迟1秒
    );
    
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取评估详情失败'
    };
  }
};

// 创建评估
export const createEvaluation = async (data: Omit<SupplierEvaluation, 'id' | 'createdAt' | 'updatedAt'>) => {
  try {
    const result = await http.post('/procurement/evaluations', data);
    return {
      success: true,
      data: result
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '创建评估失败'
    };
  }
};

// 更新评估
export const updateEvaluation = async (id: number, data: Partial<SupplierEvaluation>) => {
  try {
    const result = await http.put(`/procurement/evaluations/${id}`, data);
    return {
      success: true,
      data: result
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '更新评估失败'
    };
  }
};

// 删除评估
export const deleteEvaluation = async (id: number) => {
  try {
    const result = await http.delete(`/procurement/evaluations/${id}`);
    return {
      success: true,
      data: result
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '删除评估失败'
    };
  }
};

// 获取供应商评估统计
export const getSupplierEvaluationStats = async (supplierId: number) => {
  try {
    // 使用重试机制获取供应商评估统计
    const response = await requestWithRetry(
      () => http.get('/procurement/evaluations/stats', { params: { supplierId } }),
      3, // 最多重试3次
      1000 // 基础延迟1秒
    );
    
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取供应商评估统计失败'
    };
  }
};